package com.slkj.shilixiaoyuanapp.net.callback;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.SlxyApplication;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.net.CommonResult;
import com.slkj.shilixiaoyuanapp.net.HttpConfig;
import com.slkj.shilixiaoyuanapp.view.StateLayout;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadingDialog;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created  on 2018-08-15
 *
 * @author 唐开阔
 * @describe
 */
public abstract class CommonCallBack<T> implements Observer<CommonResult<T>> {
    private boolean isShowDialog;
    private Context context;
    private String mes = "加载中请稍后";
    StateLayout stateLayout;
    LoadingDialog dialog;

    public CommonCallBack() {
    }

    public CommonCallBack( Context context) {
        this(false,context);
    }
    public CommonCallBack(boolean isShowDialog, Context context) {
        this.isShowDialog = isShowDialog;
        this.context = context;    }

    public CommonCallBack(boolean isShowDialog, Context context, String mes) {
        this.isShowDialog = isShowDialog;
        this.context = context;
        this.mes = mes;

    }

    public CommonCallBack(StateLayout stateLayout) {
        this.stateLayout = stateLayout;
        context = stateLayout.getContext();
    }
    public CommonCallBack(boolean isShowDialog,StateLayout stateLayout) {
        this.stateLayout = stateLayout;
        this.isShowDialog = isShowDialog;
        context = stateLayout.getContext();
    }
    @Override
    public void onSubscribe(Disposable d) {
        if (isShowDialog) {
            dialog = new LoadingDialog(context);
            dialog.setImg(R.drawable.loading);
            dialog.setMsg("加载中请稍后");
            dialog.show();
        }
    }

    @Override
    public void onNext(CommonResult<T> tCommonResult) {
        if (HttpConfig.RESPONSE_OK.equals(tCommonResult.getStatus())) {
            if (stateLayout != null) {
                stateLayout.showContentView();
            }
            onCallBackSuccess(tCommonResult.getData());
        } else if (tCommonResult.getErrorCode().equals(HttpConfig.TOKEN_OVERDUE)){
            QMUIDialog dialog1 =   new QMUIDialog.MessageDialogBuilder(SlxyApplication.getSlxyApplication().getCurrentActivity())
                    .setMessage("你的账号在设备其他设备上登录。如果这不是你的操作，你的登录密码已泄露，请尽快修改")
                    .addAction("好的", (dialog, index) -> {
                        UserRequest.getInstance().exitLogon();
                    })
                    .create();
            dialog1.setCancelable(false);
            dialog1.show();
        }
        else {
            onFail(tCommonResult.getErrorMsg());
        }
    }

    @Override
    public void onError(Throwable e) {
        if (isShowDialog) {
            dialog.dismiss();
            dialog.cancel();
        }
        if (stateLayout != null) {
            stateLayout.showErrorView();
        }
        if (e instanceof SocketTimeoutException) {
            onFail("链接超时...请稍后再试");

        } else if (e instanceof ConnectException) {
            onFail("服务器连接失败...");

        } else {
            onFail(e.getMessage());
        }
    }

    @Override
    public void onComplete() {
        if (isShowDialog) {
            dialog.dismiss();
        }
    }

    /**
     * 数据请求成功
     *
     * @param data
     */
    public abstract void onCallBackSuccess(T data);

    /**
     * 数据请求失败
     *
     * @param mes
     */
    public void onFail(String mes) {
        if (context != null && stateLayout == null && isShowDialog) {
            LoadSuccessAndFailDialog.showFail(context,mes);
        }
        if (stateLayout != null) {
            stateLayout.showErrorView();
        }
    }

}
